import pyglet, game, sys
from antGP import *

SCALE = 20

ant_up_img = pyglet.image.load('ant_up_20.png')
ant_right_img = pyglet.image.load('ant_right_20.png')
ant_down_img = pyglet.image.load('ant_down_20.png')
ant_left_img = pyglet.image.load('ant_left_20.png')
cookie = pyglet.image.load('cookie_20.png')

def get_image(ch):
    if ch == 'A':
        return ant_up_img
    elif ch == '>':
        return ant_right_img
    elif ch == 'V':
        return ant_down_img
    elif ch == '<':
        return ant_left_img
    else:
        raise Exception('Unknown ant direction')

def index_to_coord(idx):
    return idx % game.SIDE * SCALE, (game.SIDE * game.SIDE -1  - idx) / game.SIDE * SCALE

def quad(x1, y1, x2, y2):
    pyglet.graphics.draw(4, pyglet.gl.GL_QUADS, ('v2f', (x1, y1, x1, y2, x2, y2, x2, y1)))

def line(x1, y1, x2, y2):
    pyglet.graphics.draw(2, pyglet.gl.GL_LINES, ('v2f', (x1, y1, x2, y2)))

def rect(x1, y1, x2, y2):
    line(x1, y1, x1, y2)
    line(x1, y2, x2, y2)
    line(x2, y2, x2, y1)
    line(x2, y1, x1, y1)

def grid():
    for i in range(game.SIDE * game.SIDE):
        x1 = ((i%game.SIDE)*SCALE)+1
        y1 = ((i/game.SIDE)*SCALE)+1
        x2 = x1 + SCALE+1
        y2 = y1 + SCALE+1
        rect(x1, y1, x2, y2)

def draw_ants(board):
    for idx, o in enumerate(board):
        if type(o) == game.Ant:
            xy = index_to_coord(idx)
            img = get_image(o.direction[0][0])
            ant = pyglet.sprite.Sprite(img, xy[0], xy[1])
            ant.scale = float(SCALE)/float(img.width)
            ant.draw()
        if type(o) == game.Morsel:
            xy = index_to_coord(idx)
            m = pyglet.sprite.Sprite(cookie, xy[0], xy[1])
            m.scale = float(SCALE)/float(cookie.width)
            m.draw()

def test():
    board = game.init_board()
    window = pyglet.window.Window(width=(game.SIDE*SCALE+2), height=(game.SIDE*SCALE+2))
    @window.event
    def on_draw():
        window.clear()
        draw_ants(board)
    def update(df):
        window.clear()
        i_board = tuple(board)
        for a in i_board:
            if type(a) == game.Ant:
                a.advance()
                a.advance()
                a.turn_right()
                a.advance()
                a.turn_left()
                a.turn_left()
                a.advance()
        draw_ants(board)
    pyglet.gl.glClearColor(230.0/255.0, 180.0/255.0, 130.0/255.0, 1)
    pyglet.clock.schedule_interval(update, 1.0)
    pyglet.app.run()

if __name__ == '__main__':
    if len(sys.argv) >= 2 and sys.argv[1] == 'test':
        test()
    else:
        antProblem = antGP()
        antProblem.run_ga(1)
        best_chromosome = antProblem.best_individual()
        print best_chromosome
        chromosome_fun = make_antfun(best_chromosome)
        board = game.init_board()
        window = pyglet.window.Window(width=(game.SIDE*SCALE+2), height=(game.SIDE*SCALE+2))
        @window.event
        def on_draw():
            window.clear()
            draw_ants(board)
        def update(df):
            window.clear()
            i_board = tuple(board)
            for a in i_board:
                if type(a) == game.Ant:
                    chromosome_fun(a)
            draw_ants(board)
        pyglet.gl.glClearColor(230.0/255.0, 180.0/255.0, 130.0/255.0, 1)
        pyglet.clock.schedule_interval(update, 0.3)
        pyglet.app.run()











